class Solution {
public:
    typedef pair<int, int> PII;
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int>mp;
        for (auto x : nums) {
            mp[x]++;
        }
        vector<int>ans;
        priority_queue<PII>q;
        for (auto it = mp.begin(); it != mp.end(); it++) {
            q.push({ it->second,it->first });
        }
        while (k--) {
            ans.push_back(q.top().second);
            q.pop();
        }
        return ans;
    }
};